#ifndef TIMESTAMP
#define TIMESTAMP
//#include"stdafx.h"

class timestamp
	{
		int year;
		int mon;
		double day;
		double mydate;
	public:
        timestamp(){
			mydate=0;
			set(mydate);
		}
		timestamp(int iyear, int imon, double iday)
			{
				/*if (iyear<0)||(iyear>>2300)) err::err2("timestamp::set_date: year has a strange value");
				if(imon<1)||(imon>12)) err::err2("timestamp::set_date:mon is ill defined");
				if(iday<1)||(iday>Basics::get_mondur(year,mon))err::err2("timestamp::set_date: day is ill defined");
				year=iyear; mon=imon;day=iday;
				mydate=Basics_MyDate::Mydate_from_GD(year,mon,day);*/
				set(iyear,imon,iday);
				
			}
		timestamp(double imydate){
			/*mydate=imydate;
			year=0;mon=0;day=0;
			if(Basics_MyDate::GD_from_Mydate(imydate,year,mon,day)>0)err::err2("timestamp::timestamp: mydate is ill defined");*/
			set(imydate);}
		timestamp(const timestamp &time)
			  {
				this->mydate=time.get_mydate();
				this->set(mydate);
			  }
        ~timestamp(){;}

		timestamp& operator=(const timestamp &time) 
		  {
				this->mydate=time.get_mydate();
				this->set(time.get_mydate());
                                return *this;
		  }
                timestamp& operator+(const timestamp &time) 
		  {
				
				this->set(this->mydate+time.get_mydate());
                                return *this;
		  }
                timestamp& operator-(const timestamp &time) 
		  {
				
				this->set(this->mydate-time.get_mydate());
                                return *this;
		  }
        int operator==(const timestamp &time) const
        {
            double tol=0.00001;
            int dec=1;
            if ((this->mydate-tol)>time.mydate) dec=0;
            if((this->mydate+tol)<time.mydate)dec=0;
            return dec;
        }
        int operator<(const timestamp &time) const
        {
            double tol=0.00001;
            int dec=1;
            if((this->mydate+tol)>=time.mydate)dec=0;
            return dec;
        }
        int operator>(const timestamp &time) const
        {
            double tol=0.00001;
            int dec=1;
            if((this->mydate-tol)<=time.mydate)dec=0;
            return dec;
        }
		int operator>=(const timestamp &time) const
        {
            double tol=0.00001;
            int dec=1;
            if((this->mydate+tol)<=time.mydate)dec=0;
            return dec;
        }
		int operator<=(const timestamp &time) const
        {
            double tol=0.00001;
            int dec=1;
            if((this->mydate-tol)>=time.mydate)dec=0;
            return dec;
        }
        int operator!=(const timestamp &time) const
        {
            return !(this->operator ==(time));
        }



		
		int check_myself()
		{
			int dec=0;
			if (mydate<0) {
				dec=dec+1;
				err::err2("timestamp::checkmyself:date is negative.");}
			if ((mon<1)||(mon>12)) {
				dec++;
				err::err2("timestamp::checkmyself:mon out of range");}
			if((day<1)||(day>Basics_MyDate::get_mondur(year,mon)+1)){
				dec++;
				err::err2("timestamp::checkmyself:day out of range");}
			return dec;

		}

		 // get
		double get_mydate() const
			{
				return mydate;
			}
        int get_year() const
        {
            return year;
        }
        int get_mon() const
        {
            return mon;
        }
        double get_day() const
        {
            return day;
        }
		double get_dist (const timestamp ref) const
        {
            return (ref.get_mydate()-this->mydate);
        }

		 // set
		int set (int iyear, int imon, double iday)
		{
			if ((iyear<0)||(iyear>2300)) err::err3("timestamp::set_date: year has a strange value");
			if((imon<1)||(imon>12)) err::err3("timestamp::set_date:mon is ill defined");
			if((iday<1)||(iday>Basics_MyDate::get_mondur(iyear,imon)))err::err2("timestamp::set_date: day is ill defined");
			year=iyear; mon=imon;day=iday;
			mydate=Basics_MyDate::Mydate_from_GD(year,mon,day);
			return check_myself();
		}
		int set(double imydate)
		{
			mydate=imydate;
			year=0;mon=0;day=0;
			if(Basics_MyDate::GD_from_Mydate(imydate,year,mon,day)>0)err::err2("timestamp::timestamp: mydate is ill defined");
            return check_myself();
		}
        int setday(double newday)
        {
			 return set(year,mon,newday);
        }
		timestamp& set_floor_day(timestamp t1)
		{
			
			this->mydate=floor(t1.mydate);
			this->set(this->mydate);
			return *this;
		}
		timestamp& set_floor_mon(timestamp t1)
		{
			 this->set(t1.get_year(),t1.get_mon(),1.0);
			 return *this;
		}
		timestamp& set_floor_year(timestamp t1)
		{
			this->set(t1.get_year(),1,1.0);
			return *this;
		}
		int set_add(double diff)
		{
				return set(mydate+diff);
		}
		int set_add_days(int diff)
		{
			int i;
			i=(int) mydate;
			mydate=(double)(diff+i);
			return set(mydate);
		}
        int set_add_months (int diff)
        {
            int i;
            int dec;
            if(diff<0)i=-1;else i=1;
            while(diff!=0){
                dec=2;
                    if((this->mon+i)>12) dec=0;
                    if((this->mon+i)<1) dec=1;
                switch(dec)
                {
                    case 0: mon=1;year++;break;
                    case 1: mon=12;year--;break;
                    default: mon=mon+i;break;
                }
                diff=diff-i;
			}
			while(day>(1+Basics_MyDate::get_mondur(this->year,this->mon))){
				day--;
			}
			if((mon==2)&&((year%4)!=0)&&(day==29)){
				day=28;}
            this->set(year,mon,day);       
            return check_myself();

        }
        int set_add_years(int diff){
			year=year+diff;
			if((mon==2)&&((year%4)!=0)&&(day==29)){
				day=28;}
			this->set(year,mon,day);
			return check_myself();
		}
	};


//istream& operator>>(istream& sin,timestamp& val)
//{
//	int year,mon;
//	double day;
//	sin>>year>>mon>>day;
//	val.set(year,mon,day);
//  return sin;
//}

#endif
